package com.fastbee.api.controller;

import com.fastbee.api.client.Amap;
import com.fastbee.api.client.Cn12306;
import com.fastbee.api.client.Gitee;
import com.fastbee.api.client.Yunchache;
import com.fastbee.api.model.*;
import com.fastbee.api.utils.Constants;
import com.fastbee.api.utils.MD5Utils;
import com.dtflys.forest.http.ForestResponse;
import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
@RestController
public class ForestExampleController {

    @Resource
    private Amap amap;

    @Resource
    private Gitee gitee;

    @Resource
    private Cn12306 cn12306;

    @Resource
    private Yunchache yunchache;


    @GetMapping("/amap/location")
    public Result<Location> amapLocation(@RequestParam BigDecimal longitude, @RequestParam BigDecimal latitude) {
        Result<Location> result = amap.getLocation(longitude.toEngineeringString(), latitude.toEngineeringString());
        return result;
    }

    @GetMapping("/amap/location2")
    public Map amapLocation2(@RequestParam BigDecimal longitude, @RequestParam BigDecimal latitude) {
        Coordinate coordinate = new Coordinate(
                longitude.toEngineeringString(),
                latitude.toEngineeringString());
        Map result = amap.getLocation(coordinate);
        return result;
    }

    @GetMapping("/amap/location3")
    public Map amapLocation3(@RequestParam BigDecimal longitude, @RequestParam BigDecimal latitude) {
        Coordinate coordinate = new Coordinate(
                longitude.toEngineeringString(),
                latitude.toEngineeringString());
        Map result = amap.getLocationByCoordinate(coordinate);
        return result;
    }

    @GetMapping("/gitee")
    public String gitee() {
        String result = gitee.index();
        return result;
    }


    @GetMapping("/gitee/async")
    public String aysncGitee() throws ExecutionException, InterruptedException {
        Future<String> future = gitee.asyncIndex();
        return future.get();
    }

    @GetMapping("/gitee/async2")
    public String aysncGitee2() throws ExecutionException, InterruptedException {
        AtomicReference<String> ref = new AtomicReference<>("");
        CountDownLatch latch = new CountDownLatch(1);
        gitee.asyncIndex2((result, request, response) -> {
            ref.set(result);
            latch.countDown();
        }, (ex, request, response) -> {
            ref.set(ex.getMessage());
            latch.countDown();
        });
        latch.await();
        return ref.get();
    }



    @GetMapping("/12306")
    public String cn12306() {
        ForestResponse<String> response = cn12306.index();
        return response.getResult();
    }


    @GetMapping("/gitee/branches")
    public List<GiteeBranch> giteeBranches(@RequestParam String accessToken,
                                           @RequestParam String owner,
                                           @RequestParam String repo) {
        List<GiteeBranch> branches = gitee.branches(accessToken, owner, repo);
        return branches;
    }

    @GetMapping("/gitee/readme")
    public GiteeReadme giteeReadme(@RequestParam String accessToken,
                                         @RequestParam String owner,
                                         @RequestParam String repo,
                                         @RequestParam String ref) {
        GiteeReadme readme = gitee.readme(accessToken, owner, repo, ref);
        return readme;
    }

    @GetMapping("/yunchache/gps_r")
    public MapMarker yunchache_gps_r() {
        Map<String, Object> base = Constants.getComParams();
        Map<String, Object> params = new HashMap<>();
        params.put("userId", Constants.userId);
        params.put("password", MD5Utils.getmd5("888"));
        ForestResponse<Map> response = yunchache.login(base,params);
        base.put("sessionId", response.getResult().get("sessionId").toString());
        params.clear();
        params.put("simple", "true");
        ForestResponse<YccResult<List<Gps_r>>> resp = yunchache.getGpsR(base, params);
        MapMarker mapMarker = new MapMarker();
        List<MapMarker.marker> list = new ArrayList<>();
        resp.getResult().getList().forEach(
                gps_r -> {
                    log.info("gps_r:{} {}", gps_r.getBlat(), gps_r.getBlng());
                    MapMarker.marker marker = new MapMarker.marker();
                    marker.setName(gps_r.getCarName());
                    marker.setValue(10);
                    Float lng = Float.parseFloat(gps_r.getGlng());
                    Float lat = Float.parseFloat(gps_r.getGlat());
                    List<Float> position = new ArrayList<>();
                    position.add(lng);
                    position.add(lat);
                    marker.setPosition(position);
                    list.add(marker);
                }
        );
        mapMarker.setMarkers(list);
        return mapMarker;
    }

}
